Public Class Mario
    Inherits Sprite

    Public Enum MarioState
        Standing
        Walking
        Ducking
        Jumping
        Stopping
    End Enum

    Public Enum SpriteDirection
        Left
        Right
    End Enum

    Private _state As MarioState
    Private _waitWalk As Double
    Private isJumping As Boolean
    Private isJStopping As Boolean
    Private isDucking As Boolean
    Private isJumpingUp As Boolean
    Private intjumpTop As Integer = 0
    Private jumpHeight As Integer = 64
    Public currentGround As Integer = 340
    Public jumpStart As Integer = 300
    Public moving As Boolean = False
    Private lastImage As String = "SMarioStanding"
    Private intFrame As Integer
    Private intFrameLimit As Integer = 3

    Private isStopping As Boolean
    Private isWalking As Boolean
    Private dis As Single = 1
    Private vel As Single = 1

    Private images As New Collection()

    Private objMap As Map

    Public direction As SpriteDirection = SpriteDirection.Right

    Private Const IMG_FOLDER As String = "../../Content/Images/"

    Public Property State() As MarioState
        Get
            Return _state
        End Get
        Set(ByVal Value As MarioState)
            _state = Value
        End Set
    End Property

    Public Sub New(ByVal imagePath As String, ByVal location As Point, ByVal transparentColor As Color, ByVal foreground As Map)
        MyBase.New(imagePath, location, transparentColor)
        objMap = foreground
        LoadImages()
        Me.Bitmap = images("SMarioStanding")
    End Sub

    Private Sub LoadImages()
        images.Add(New Bitmap(IMG_FOLDER & "marioLS.png"), "LMarioStanding")
        images.Add(New Bitmap(IMG_FOLDER & "marioLR1.png"), "LMarioRunning1")
        images.Add(New Bitmap(IMG_FOLDER & "marioLR2.png"), "LMarioRunning2")
        images.Add(New Bitmap(IMG_FOLDER & "marioLJ.png"), "LMarioJumping")
        images.Add(New Bitmap(IMG_FOLDER & "marioLJ2.png"), "LMarioJumping2")
        images.Add(New Bitmap(IMG_FOLDER & "marioLD.png"), "LMarioDucking")
        images.Add(New Bitmap(IMG_FOLDER & "Smario.png"), "SMarioStanding")
        images.Add(New Bitmap(IMG_FOLDER & "SmarioR1.png"), "SMarioRunning1")
        images.Add(New Bitmap(IMG_FOLDER & "SmarioJ1.png"), "SMarioJumping1")
        images.Add(New Bitmap(IMG_FOLDER & "SmarioJ2.png"), "SMarioJumping2")
    End Sub

    Public Overloads Sub Draw(ByRef e As System.Windows.Forms.PaintEventArgs, ByVal elapsedTime As Long)
        Update(elapsedTime)
        UpdateMap()
        MyBase.Draw(e)
        e.Graphics.DrawString(currentGround, New Font("Courier New", 12), Brushes.Black, 0, 24)
    End Sub

    Private Sub UpdateMap()
        If Me.ptLocation.X >= 17 * 32 Then
            objMap.left -= Me.ptLocation.X - (17 * 32)
            Me.ptLocation.X -= Me.ptLocation.X - (17 * 32)
        ElseIf Me.ptLocation.X <= 64 Then
            objMap.left += 64 - Me.ptLocation.X
            Me.ptLocation.X += 64 - Me.ptLocation.X
        End If
    End Sub

    Private Sub Update(ByVal elapsedTime As Double)
        'If Me.location.X > frmMain.Width Then
        '    Me.ptLocation.X = -Me.Bitmap.Width
        'End If
        'If Me.location.X < (-Me.Bitmap.Width) Then
        '    Me.ptLocation.X = frmMain.Width
        'End If

        currentGround = objMap.getCurrentGround(Me) - Me.Bitmap.Height

        If Not isJumping And currentGround > Me.location.Y + Me.Bitmap.Height Then
            isJumping = True
            isJumpingUp = False
            isJStopping = True
        End If

        If Me.State = MarioState.Jumping Or isJumping Then
            UpdateCharacterJumping(elapsedTime)
        ElseIf Me.State = MarioState.Walking Or isWalking Then
            isWalking = True
            UpdateCharacterWalking(elapsedTime)
            'ElseIf Me.State = MarioState.Ducking Then
        ElseIf Me.State = MarioState.Stopping Or isStopping Then
            isWalking = False
            isStopping = True
            UpdateCharacterStopping(elapsedTime)
        ElseIf Me.State = MarioState.Standing Then
            Me.Bitmap = images("SMarioStanding")
            lastImage = "LMarioStanding"
            intFrame = 0
            moving = False
            isWalking = False
            isStopping = False
            If Me.direction = SpriteDirection.Left Then flipMe()
        End If
        If Not (isJStopping OrElse Me.State = MarioState.Jumping) Then
            Me.ptLocation.Y = currentGround
        End If
    End Sub


    'Updates mario's jumping
    Private Sub UpdateCharacterJumping(ByVal elapsedTime As Double)
        If isJStopping = False And Me.location.Y <= currentGround - (Me.Bitmap.Height * 1.5) Then
            stopjump()
        End If
        If Not isJumping Then
            isJumping = True
            isJumpingUp = True
        End If
        If Not isJStopping Then
            Me.Bitmap = images("SMarioJumping1")
            Me.location -= New Point(0, elapsedTime / 25000)
        End If
        If isJStopping Then
            If Me.location.Y > jumpStart - jumpHeight AndAlso isJumpingUp Then
                Me.Bitmap = images("SMarioJumping1")
                Me.location -= New Point(0, elapsedTime / ((((jumpStart - Me.location.Y) / jumpHeight) * 70000) + 15000))
            Else
                Me.Bitmap = images("SMarioJumping2")
                isJumpingUp = False
                If Me.location.Y < jumpStart Then
                    Me.location += New Point(0, elapsedTime / ((((jumpStart - Me.location.Y) / jumpHeight) * 30000) + 15000))
                Else
                    Me.location += New Point(0, elapsedTime / 25000)
                End If


                If Me.location.Y >= currentGround Then
                    Me.ptLocation.Y = currentGround
                    isJumping = False
                    isJStopping = False
                    Me.Bitmap = images("SMarioStanding")
                    If moving Then
                        If isStopping Then
                            Me.State = MarioState.Stopping
                        ElseIf isWalking Then
                            Me.State = MarioState.Walking
                        End If
                    Else
                        Me.State = MarioState.Standing
                    End If
                End If
            End If
        End If

        If Me.direction = SpriteDirection.Left Then flipMe()
        If moving Then
            If isWalking Then
                UpdateCharacterWalking(elapsedTime)
            ElseIf isStopping Then
                UpdateCharacterStopping(elapsedTime)
            End If
        End If

    End Sub


    Private Sub UpdateCharacterWalking(ByVal elapsedTime As Double)
        If Not isJumping Then animate()
        Dim move As Double = elapsedTime / 50000
        Select Case Me.direction
            Case SpriteDirection.Right
                Dim canmove As Integer = objMap.canMoveRight(Me, move)
                If canmove = 0 Then
                    Me.location += New Point(move, 0)
                Else
                    Me.location = New Point(canmove - Me.Bitmap.Width, Me.location.Y)
                    vel = 1
                End If
            Case SpriteDirection.Left
                Dim canmove As Integer = objMap.canMoveLeft(Me, move)
                If canmove = 0 Then
                    Me.location -= New Point(move, 0)
                Else
                    Me.location = New Point(canmove, Me.location.Y)
                    vel = 1
                End If
        End Select
    End Sub

    Public Sub smoving()
        isWalking = True
        vel += 1   'Adds to the time mario has been walking
    End Sub
    Public Sub stoporslow()    'Only called when left or right keyup 
        If vel > 4 Then     'Checks to make sure how long Mario has been walking
            stopping()
            Me.State = MarioState.Stopping
            vel = 1
        Else
            moving = False
            Me.State = MarioState.Standing
            vel = 1
            isWalking = False
            isStopping = False
        End If
    End Sub

    Public Sub ducks()
        isDucking = True
    End Sub
    Public Sub stopjump()
        If isJStopping = False Then
            isJStopping = True
            jumpStart = Me.location.Y
        End If
    End Sub
    Public Sub stopping()
        isStopping = True
        isWalking = False
    End Sub

    Private Sub UpdateCharacterStopping(ByVal elapsedTime As Double)
        If Not isJumping Then animate()
        Select Case Me.direction
            Case SpriteDirection.Right
                vel = Math.Sqrt(25 - (dis * dis))
                If vel > 0 Then
                    Me.location += New Point(vel, 0)
                Else
                    vel = 0
                End If
            Case SpriteDirection.Left
                vel = Math.Sqrt(25 - (dis * dis))
                If vel > 0 Then
                    Me.location -= New Point(vel, 0)
                Else
                    vel = 0
                End If
        End Select
        dis += 1
        If vel = 0 Then
            Me.State = MarioState.Standing
            vel = 0
            dis = 0
            moving = False
            isStopping = False
            isWalking = False
        End If

    End Sub


    Private Sub animate()
        intFrame += 1
        If intFrame > intFrameLimit Then
            If moving Then
                Select Case lastImage
                    Case "LMarioStanding"
                        Me.Bitmap = images("SMarioRunning1")
                        lastImage = "LMarioRunning1"
                        If Me.direction = SpriteDirection.Left Then flipMe()
                    Case "LMarioRunning1"
                        Me.Bitmap = images("SMarioStanding")
                        lastImage = "LMarioStanding"
                        If Me.direction = SpriteDirection.Left Then flipMe()
                End Select
            End If
            intFrame = 0
        End If
    End Sub

    Private Sub flipMe()
        Dim newBmp As Bitmap = Me.Bitmap.Clone
        newBmp.RotateFlip(RotateFlipType.RotateNoneFlipX)
        Me.Bitmap = newBmp.Clone
    End Sub

End Class
